<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\common\exception\UploadException;
use app\common\library\Upload;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Reader\Csv;
use PhpOffice\PhpSpreadsheet\Reader\Xls;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use think\Db;
use think\Exception;
use think\exception\PDOException;

/**
 * 图书管理
 *
 * @icon fa fa-circle-o
 */
class Books extends Backend
{

    /**
     * Books模型对象
     * @var \app\admin\model\Books
     */
    protected $model = null;
    /**
     * 快速搜索时执行查找的字段
     */
    protected $searchFields = 'id,title';
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Books;

    }


    /**
     * 导入出库单
     */
    public function importbooks(){
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }

        $category_id          = $this->request->param('category_id',0);
        if(!$category_id){
            $this->error('请选择所属分类');
        }


        $file = $this->request->request('file');
        if (!$file) {
            $this->error(__('Parameter %s can not be empty', 'file'));
        }
        $files = explode(',',$file);

        foreach ($files  as  $val){
//            $filePath = ROOT_PATH . DS . 'public' . DS . $val;
//            if (!is_file($filePath)) {
//                $this->error(__('No results were found'));
//            }
            //实例化reader
            $file_name = $this->get_pdf_basename($val);
            $file_name = $this->get_txt_basename($file_name);
            $file_name = $this->get_doc_basename($file_name);
            $insert_all[] = [
                'category_id' => $category_id,
                'title'       => $file_name,
                'url'         => $val,
                'createtime'  => time(),
                'updatetime'  => time()
            ];
        }

        try{
            $this->model->insertAll($insert_all);
        }catch (\Exception $e){
            Db::rollback();
            $this->error($e->getMessage());
        }


        Db::commit();
        $this->success();

    }


    public function get_pdf_basename($filename){
        $filename  =  preg_replace('/^.+[\\\\\\/]/', '', $filename);
        $filenames = explode('.pdf',$filename);
        return $filenames[0];
    }
    public function get_txt_basename($filename){
        $filenames = explode('.txt',$filename);
        return $filenames[0];
    }
    public function get_doc_basename($filename){
        $filenames = explode('.doc',$filename);
        return $filenames[0];
    }
    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }

        //如果发送的来源是Selectpage，则转发到Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();

        $list = $this->model
                ->with(['category'])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
        $result = array("total" => $list->total(), "rows" => $list->items());

        return json($result);

    }

}
